package com.yr.ryjk.doctor;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.doctor.entity.Doctor;
import com.yr.ryjk.doctor.entity.DoctorRegister;
import com.yr.ryjk.doctor.entity.FilterParam;
import com.yr.ryjk.doctor.service.IDoctorService;
import com.yr.ryjk.doctor.vo.TotalCensusVO;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.util.JSONUtil;

/**
 * 医生
 * @author Rick
 *
 */
@Controller
@RequestMapping("/doctor")
public class DoctorController {
	private Logger logger=Logger.getLogger(this.getClass());
	@Autowired
	private IDoctorService doctorService;
	
	/**
	 * 新增医生
	 * @param doctor
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addDoctor(@ModelAttribute @Valid Doctor doctor){		
		 return doctorService.addDoctor(doctor);
	}
	
	/**
	 * 修改医生
	 * @param doctor
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/modify",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyDoctor(Doctor doctor){		
		 doctorService.modifyDoctorInfo(doctor);
		 return JSONUtil.getJSON(ErrorType.SUCCESS);		 
	}
	
	/**
	 * 医生注册第一步,不拦截
	 * @param phone
	 * @param password
	 * @param checkcode
	 * @return
	 */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> register(@RequestParam("phone") @NotBlank String phone,
			@RequestParam("password") @NotBlank String password,
			@RequestParam("checkcode") @NotBlank String checkcode){
		return doctorService.addRegister(phone,password,checkcode);
	}
	
	/**
	 * 医生注册第二步,不拦截
	 * @param dg
	 * @return
	 */
	@RequestMapping(value="/submitData",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> submitData(@ModelAttribute @Valid DoctorRegister dg){
		doctorService.addRegister2(dg);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 医生注册第三步,不拦截
	 * @return
	 */
	@RequestMapping(value="/submitPics",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> submitPics(@RequestParam("phone") @NotBlank String phone,
			@RequestParam("halfPicUrl") @NotBlank String halfPicUrl,
			@RequestParam("cardUrl") @NotBlank String cardUrl,
			@RequestParam("workProofPicUrl") @NotBlank String workProofPicUrl){
		doctorService.addRegister3(phone, halfPicUrl, cardUrl,workProofPicUrl);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 医生登录接口,不拦截
	 * @param phone
	 * @param password
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(@RequestParam("phone") @NotBlank String phone,
			@RequestParam("password") @NotBlank String password,
			@RequestParam("jpushKey") @NotBlank String jpushKey) throws IllegalAccessException, InvocationTargetException{
		return doctorService.updatelogin(phone,password,jpushKey);
	}
	
	/**
	 * 重置密码
	 * @param phone
	 * @param newPassword
	 * @param checkcode
	 * @return
	 */
	@RequestMapping(value="/resetPassword",method=RequestMethod.POST)
	@ResponseBody 
	public Map<String, Object> resetPassword(@RequestParam("phone") @NotBlank String phone,
			@RequestParam("newPassword") @NotBlank String newPassword,
			@RequestParam("checkcode") @NotBlank String checkcode) {
		return doctorService.updateResetPassword(phone,newPassword,checkcode);
	}
	
	/**
	 * 用户主动修改密码
	 * @param oldPassword 旧密码
	 * @param newPassword 新密码
	 * @param token 令牌
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/modifyPassword",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyPassword(@RequestParam("oldPassword") @NotBlank String oldPassword,
			@RequestParam("newPassword") @NotBlank String newPassword,
			@RequestParam("token") @NotBlank String token) {
		return doctorService.updatePassword(oldPassword,newPassword,token);
	}
	
	/**
	 * 过滤分类查询医生
	 * @param pageNo
	 * @param pageSize
	 * @param typeId
	 * @param name
	 * @param phone
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/listByType",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listByType(@RequestParam(value="pageNo",defaultValue="1")int pageNo,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize,
			@RequestParam(value="typeId")String typeId,
			String name,
			String phone){
		Map<String, Object> map=doctorService.selectByType(pageNo,pageSize,typeId,name,phone);
		return JSONUtil.getJSON(ErrorType.SUCCESS,map);
	}
	
	/**
	 * 后台分页获取医生列表
	 * @param pageNo
	 * @param pageSize
	 * @param status医生帐号审核状态 
	 * @param name 查询条件
	 * @param phone 查询条件
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody 
	public Map<String, Object> list(@RequestParam(value="pageNo",defaultValue="1")int pageNo,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize,
			@RequestParam(value="status",defaultValue="0")int status,
			@RequestParam(value="isDown")String isDown,
			@RequestParam("name")String name,
			@RequestParam("phone")String phone) {
		Map<String, Object> map=doctorService.getListOfPage(pageNo,pageSize,status,name,phone,isDown);
		return JSONUtil.getJSON(ErrorType.SUCCESS,map);
	}
	
	/**
	 * 审核医生注册账户
	 * @param ids id集合,逗号分割
	 * @param action 操作,0为审核通过,1为审核驳回
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/check",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> check(@RequestParam(value="ids") @NotBlank String ids,
			@RequestParam(value="action") @NotNull Integer action,
			String reason) throws IllegalAccessException, InvocationTargetException {
		doctorService.updateCheck(ids,action,reason);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 根据id获取详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/get",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getInfo(@RequestParam(value="id") @NotBlank String id) {
		try {
			return JSONUtil.getJSON(ErrorType.SUCCESS,doctorService.getDoctorById(id));
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
		}
	}
	
	/**
	 * app根据科室获取医生
	 * @param id 科室id
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/listDepartment",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listDepartment(@RequestParam(value="pageNo",defaultValue="1") @NotBlank int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") @NotBlank int pageSize,
			@RequestParam("token") @NotBlank String token,
			FilterParam filterParam) {
		return doctorService.getListOfDepartment(pageNo,pageSize,token,filterParam);
	}
	
	/**
	 * 增加浏览次数(按科室查看和名医推荐都需要调用此接口)
	 * @param doctorId
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/increaseScanTimes",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> increaseScanTimes(@RequestParam(value="id") @NotBlank String doctorId) {
		doctorService.updateIncreaseScanTimes(doctorId);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 获取统计信息
	 * @param token
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/census",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> census(@RequestParam(value="token") @NotBlank String token) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,doctorService.getCensusData(token));
	}
	
	/**
	 * 根据关键字搜索医生接口
	 * @param pageNo
	 * @param pageSize
	 * @param keyword 可能是医生名字，科室名称或医院名称
	 * @param token
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/search",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> search(@RequestParam(value="pageNo",defaultValue="1") @NotBlank int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") @NotBlank int pageSize,
			@RequestParam("keyword") @NotBlank String keyword,
			@RequestParam(value="token") @NotBlank String token,
			FilterParam filterParam) {
		return doctorService.selectSearch(pageNo,pageSize,keyword,token,filterParam);
	}
	
	/**
	 * app医生修改个人咨询价格
	 * @param askPrice
	 * @param token
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/modifyAskPrice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyAskPrice(@RequestParam("askPrice") int askPrice,
			@RequestParam(value="token") @NotBlank String token) {
		doctorService.updateAskPrice(askPrice,token);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 添加医生至某个类别
	 * @param ids
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/addDocInGroup",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addDocInGroup(@RequestParam("ids") String ids,@RequestParam("group") String group){
		try {
			doctorService.modifyDocInGroup(ids,group);
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
		}
	}
	
	/**
	 * 后台web获取医生的统计信息
	 * @param docId
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/getTotalCensus",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTotalCensus(@RequestParam("docId") String docId){
		try {
			TotalCensusVO vo=doctorService.getTotalCensus(docId);
			return JSONUtil.getJSON(ErrorType.SUCCESS,vo);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
		}
	}
	/**
	 * 医生入驻管理
	 * @param docId
	 * @param isDown
	 * @return
	 */
	
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/dactorDown",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> doctorDown(@RequestParam("docId") String docId,@RequestParam("isDown") int isDown){
		try {
			doctorService.modifyDoctorDown(docId,isDown);
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
		}
	}
}